package com.coocaa.ops.admin.v1.policygroup.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.admin.v1.policygroup.handler.MatchHomeHandler;
import com.coocaa.ops.admin.v1.policygroup.model.CheckModelChipDataModel;
import com.coocaa.ops.admin.v1.policygroup.model.HomeMatchQueryModel;
import com.coocaa.ops.admin.v1.policygroup.model.query.PolicyGroupInfoModel;
import com.coocaa.ops.admin.v1.policygroup.provider.IPolicyGroupInfoQueryProvider;
import com.coocaa.ops.admin.v1.policygroup.service.IPolicyGroupInfoQueryService;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.cache.constant.InternalCacheOpApiUrlConstants;
import com.coocaa.ops.common.core.biz.cache.yml.CacheUpdateProperties;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import com.coocaa.ops.common.tool.http.OkHttpUtil;
import com.coocaa.ops.common.tool.json.ParseJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author bjh
 * @description 策略组信息原始表-查询service接口实现
 * @date : 2020-03-12 14:46:04
 */
@Service
public class PolicyGroupInfoQueryServiceImpl implements IPolicyGroupInfoQueryService {
    @Autowired
    private IPolicyGroupInfoQueryProvider thisQueryProvider;
    @Autowired
    private CacheUpdateProperties cacheUpdateProperties;

    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Override
    public PolicyGroupInfoEntity getDetailInfo(Integer id, Integer version) {
        PolicyGroupInfoEntity entity = thisQueryProvider.getDetailInfo(id, version);
        return entity;

    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<PolicyGroupInfoEntity> pageList(PolicyGroupInfoModel model) {
        PageResultModel<PolicyGroupInfoEntity> pageResultModel = thisQueryProvider.pageList(model);
        return pageResultModel;
    }

    /**
     * 多个使用逗号分隔
     * 15U_5S02
     *
     * @param modelChip
     */
    @Override
    public List<CheckModelChipDataModel> listPolicyGroupByModelChip(String modelChip) {
        return thisQueryProvider.listPolicyGroupByModelChip(modelChip);
    }

    @Override
    public R matchHomepage(HomeMatchQueryModel model) throws Exception {
        String host = cacheUpdateProperties.getDashboard();
        String url = host + "/" + InternalCacheOpApiUrlConstants.GET_HOME_MODULES;
        Map<String, String> headerMap = MatchHomeHandler.getHeadMap(model);
        JSONObject data = getData(url, headerMap);
        return R.ok().data(data);
    }

    public JSONObject getData(String url, Map<String, String> headerMap)throws Exception {
        OkHttpUtil okHttpUtil = new OkHttpUtil();
        String result = okHttpUtil.requestGetUrlToJSON(url, null, headerMap);
        if (null == result){
            return null;
        }
        JSONObject data = ParseJsonUtil.parseJSONObject(result).getJSONObject("data");
        return data;
    }
}

